clear
rng default
ntypes_m=4;
ntypes_w=4;
all_years=1940:1:1967;
dim=20; 
% try different sizes of grid hypercube (by varying dim) as discussed in Appendix C of the paper (need powerful cluster to run the code for larger sizes of grid hypercube)

G=cell(ntypes_m,ntypes_w);
load(num2str(all_years(1)));
for i=1:ntypes_m
    for j=1:ntypes_w
            G{i,j}=[min([U_Gumbel(i,j), V_Gumbel(i,j), Phi_Gumbel(i,j)])...
                    max([U_Gumbel(i,j), V_Gumbel(i,j), Phi_Gumbel(i,j)])];
    end
end

for i=1:ntypes_m
    for j=1:ntypes_w
        for h=2:size(all_years,2)
            load(num2str(all_years(h)));
            G{i,j}=[min(...
                    min([U_Gumbel(i,j), V_Gumbel(i,j), Phi_Gumbel(i,j)]),G{i,j}(1))  ...
                    max(...
                    max([U_Gumbel(i,j), V_Gumbel(i,j), Phi_Gumbel(i,j)]),G{i,j}(2))];   
        end
    end
end

G_round=cell(ntypes_m,ntypes_w);
for i=1:ntypes_m
    for j=1:ntypes_w
        %min
        G_round{i,j}(1)=floor(G{i,j}(1)-dim); 
        %max
        if G{i,j}(2)<0
           G_round{i,j}(2)=dim;
        else
           G_round{i,j}(2)=ceil(G{i,j}(2)+dim);
        end
    end
end


save('G_round.mat', 'G_round')



